import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#打开爬取二点csv文件
data = pd.read_csv(r"C:\Users\梅欢\Desktop\spider1\positive.csv", encoding='utf-8')
plt.rcParams['font.sans-serif'] = ['SimHei']

#1.绘制总览图
universities = data['university'].tolist()
scores = data['score'].tolist()
# 创建画布和子图
plt.figure(figsize=(10, 12))  # 设置画布大小
colors = ['skyblue', 'lightgreen']
for i in range(len(universities)):
    color_index = i % 2  # 通过取余来交替使用颜色
    plt.barh(universities[i], scores[i], color=colors[color_index])
plt.xlabel('Scores')  # x轴标签
plt.title('Top 30 Chinese Universities by Scores')  # 标题
plt.gca().invert_yaxis()  # 反转y轴，让第一名显示在上方
plt.gca().xaxis.set_major_locator(plt.MultipleLocator(2.5))
plt.tight_layout()
plt.show()

#2.绘画大学类型的饼图
kind = data[['kind']]
category_counts = kind.value_counts()
plt.figure(figsize=(8, 8))
plt.pie(category_counts,autopct='%1.1f%%', startangle=90,shadow=True)
plt.legend(category_counts.index, loc="best")
plt.title('类别分布饼图')
plt.axis('equal')
plt.show()

#3.绘画地区的地区分布图
from collections import Counter
from pyecharts.charts import Map
from pyecharts import options as opts
# 原始数据
locations = data['position'].tolist()
locations = [item + '市' if item in ['北京', '上海', '天津', '重庆'] else item + '省' for item in locations]
# 统计每个地区出现的次数
location_counts = dict(Counter(locations))
# 准备地图数据
data_pair = [(k, v) for k, v in location_counts.items()]
# 绘制地图
map_chart = (
    Map()
    .add("中国顶尖大学分布图", data_pair, "china")
    .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="中国地图-地区分布"),
        visualmap_opts=opts.VisualMapOpts(max_=max(location_counts.values()))
    )
)
# 生成html文件
map_chart.render("地区分布地图.html")


#补充内容（西南交通大学五年内排名）

rating = [53, 51, 52, 55, 59]
year = [2020,2021,2022,2023,2024]
# 创建折线图
plt.figure(figsize=(8, 5))
plt.plot(year, rating, marker='o', linestyle='-', color='b', label='SWJTU')
# 添加标题和标签
plt.title('SWJTU Rating')
plt.xlabel('Years')
plt.ylabel('Rating')
plt.xticks(year)  # 设置 x 轴刻度为整数序列
plt.gca().invert_yaxis()
# 显示图例
plt.legend()
plt.tight_layout()
plt.show()

